/*
 * To change this template, choose Tools | Templates and open the template in the editor.
 */

import java.awt.Component;
import java.awt.Rectangle;
import java.io.IOException;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.media.protocol.DataSource;
import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;
import javax.tv.xlet.XletStateChangeException;

import org.havi.ui.HContainer;
import org.havi.ui.HScene;
import org.havi.ui.HSceneFactory;
import org.havi.ui.HSceneTemplate;
import org.havi.ui.HScreen;

/**
 *
 * @author root
 */
public class Video extends HContainer implements Xlet, ControllerListener {

    //private static final String VIDEO_URL_2 = "file://root/NetBeansProjects/UmVideo/build/classes/meuvideo.avi";
    //private static final String VIDEO_URL_2 = "file://D://ProjetoFederal_Ginga//federal//aulaTVD_07_04_08//projetosTVD//UmVideo//build//classes//meuvideo.avi";
    private static final String VIDEO_URL_2 = "file://f://tcc//meuvideo.avi";

    private HScene scene;

    private Player player;

    public void initXlet(XletContext arg0) throws XletStateChangeException {
        System.out.println("DENTRO ----------initXlet.........." + VIDEO_URL_2);
        criaScene();
    }

    public void criaScene() {
        System.out.println("DENTRO DE CRIASCENE................: " + scene);

        // Build a full-screen HSceneTemplate.
        HSceneTemplate hst = new HSceneTemplate();
        hst.setPreference(HSceneTemplate.SCENE_SCREEN_DIMENSION, new org.havi.ui.HScreenDimension(1, 1), HSceneTemplate.REQUIRED);
        hst.setPreference(HSceneTemplate.SCENE_SCREEN_LOCATION, new org.havi.ui.HScreenPoint(0, 0), HSceneTemplate.REQUIRED);

        // Now get our HScene.
        HSceneFactory hsceneFactory = HSceneFactory.getInstance();
        scene = hsceneFactory.getFullScreenScene(HScreen.getDefaultHScreen().getDefaultHGraphicsDevice());
        System.out.println("DENTRO DE CRIASCENE................: " + scene.toString());
        // If we can't get an HScene that meets our needs, die.
        if (scene == null) {
            try {
                throw new XletStateChangeException("Can't create HScene");
            } catch (XletStateChangeException ex) {
                ex.printStackTrace();
            }
        }

        // If we have got a suitable HScene, set our size to be the full size of
        // the HScene and add ourselves to it.
        Rectangle rect = scene.getBounds();
        setBounds(rect);
        setBounds(10, 10, 700, 556);
        scene.setVisible(false);
        scene.add(this);
        System.out.println("DENTRO DE CRIASCENE.....FIM...........: " + rect);
    }

    public void startXlet() throws XletStateChangeException {
        javax.media.MediaLocator locator;

        //Set our scene to be visible.  Later in the Xlet we will put the video 
        // in an HComponent, so we need a visible HScene in which to display it.
        System.out.println("fora de startScene...1: " + scene.getSceneTemplate());
        scene.setVisible(true);
        System.out.println("fora de startScene...2: " + scene.getSceneTemplate());

        try {
            // Create a data source for our locator.  We could create the player
            // directly by calling Manager.createPlayer() with the MediaLocator if we
            // preferred - the effect is the same.
            locator = new javax.media.MediaLocator(VIDEO_URL_2);
            DataSource playingDataSource = Manager.createDataSource(locator);
            // Now create the player
            player = Manager.createPlayer(playingDataSource);

            // Add ourselves as a listener for events from the player
            player.addControllerListener(this);

            // Start the player.  At this point, any video would be played in the 
            // background on a normal MHP or OCAP receiver.
            System.out.println("starting...");

            player.start();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MediaException e) {
            e.printStackTrace();
        }

    }

    public void pauseXlet() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void destroyXlet(boolean arg0) throws XletStateChangeException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void controllerUpdate(ControllerEvent event) {
        Component comp = null;
        if (event instanceof RealizeCompleteEvent) {
            // Try to display the video in a window (admittedly, a full-screen window)

            // Calling getVisualComponent() will convert the player from a
            // 'background player' (which plays its video in the video plane)
            // to a 'component player' (which plays its video in an AWT component).
            // This is a one-way operation, so the only way to go back is to
            // destroy the current player and create a new one.  If the operation 
            // fails, then the player will remain as a background player.
            if ((comp = player.getVisualComponent()) != null) {

                add(comp);
                System.out.println("Added " + comp + " as a video component");

                // Set the size and position (full screen, placed in the top left corner)
                // and draw it.
                comp.setSize(700, 556);
                setSize(700, 556);

            }

        } else if (event instanceof EndOfMediaEvent) {
            // We've reached the end of the media; rewind and 
            // start over.

            // Note that setMediaTime() will NOT work in this way on a real MHP
            // implementation.  The concept of 'media time' has no real meaning in a
            // broadcast environment, and so xlets can only set the media time of
            // audio clips that are played from a file.

            player.setMediaTime(new Time(0));
            player.start();
        }
    }
}
